home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / answers / comp / x-faq / part6 < prev    next >
Text File  |  1994-03-04  |  43KB  |  867 lines

  1. Newsgroups: comp.windows.x,news.answers,comp.answers
  2. Path: bloom-beacon.mit.edu!hookup!news.kei.com!MathWorks.Com!europa.eng.gtefsd.com!gatech!swrinde!cs.utexas.edu!uunet!visual!dbl
  3. From: dbl@visual.com (David B. Lewis)
  4. Subject: comp.windows.x Frequently Asked Questions (FAQ) 6/6
  5. Message-ID: <CM59M2.A9G@visual.com>
  6. Followup-To: poster
  7. Summary: useful information about the X Window System
  8. Reply-To: faq%craft@uunet.uu.net (X FAQ maintenance address)
  9. Organization: VISUAL, Inc.
  10. Date: Fri, 4 Mar 1994 14:30:02 GMT
  11. Approved: news-answers-request@MIT.Edu
  12. Expires: Sun, 3 Apr 1994 00:00:00 GMT
  13. Lines: 851
  14. Xref: bloom-beacon.mit.edu comp.windows.x:22118 news.answers:16019 comp.answers:4036
  15.  
  16. Archive-name: x-faq/part6
  17. Last-modified: 1994/03/03
  18.  
  19. ----------------------------------------------------------------------
  20. Subject: 133)  Why doesn't XtDestroyWidget() actually destroy the widget?
  21.  
  22.     XtDestroyWidget() operates in two passes, in order to avoid leaving
  23. dangling data structures; the function-call marks the widget, which is not 
  24. actually destroyed until your program returns to its event-loop. 
  25.  
  26. ----------------------------------------------------------------------
  27. Subject: 134)  How do I query the user synchronously using Xt?
  28.     
  29.     It is possible to have code which looks like this trivial callback,
  30. which has a clear flow of control. The calls to AskUser() block until answer
  31. is set to one of the valid values. If it is not a "yes" answer, the code drops
  32. out of the callback and back to an event-processing loop: 
  33.  
  34.     void quit(Widget w, XtPointer client, XtPointer call)
  35.     {
  36.         int             answer;
  37.         answer = AskUser(w, "Really Quit?");
  38.         if (RET_YES == answer)
  39.             {
  40.             answer = AskUser(w, "Are You Really Positive?");
  41.             if (RET_YES == answer)
  42.                 exit(0);
  43.                 }
  44.     }
  45.  
  46.     A more realistic example might ask whether to create a file or whether 
  47. to overwrite it.
  48.     This is accomplished by entering a second event-processing loop and
  49. waiting until the user answers the question; the answer is returned to the
  50. calling function. That function AskUser() looks something like this, where the 
  51. Motif can be replaced with widget-set-specific code to create some sort of 
  52. dialog-box displaying the question string and buttons for "OK", "Cancel" and 
  53. "Help" or equivalents:
  54.  
  55.   int AskUser(w, string)
  56.         Widget          w;
  57.         char           *string;
  58.   {
  59.         int             answer=RET_NONE;    /* some not-used marker */
  60.         Widget          dialog;            /* could cache&carry, but ...*/
  61.         Arg             args[3];
  62.         int             n = 0;
  63.         XtAppContext    context;
  64.  
  65.         n=0;
  66.         XtSetArg(args[n], XmNmessageString, XmStringCreateLtoR(string,
  67.                 XmSTRING_DEFAULT_CHARSET)); n++;
  68.         XtSetArg(args[n], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); n++;
  69.         dialog = XmCreateQuestionDialog(XtParent(w), string, args, n);
  70.         XtAddCallback(dialog, XmNokCallback, response, &answer);
  71.         XtAddCallback(dialog, XmNcancelCallback, response, &answer);
  72.         XtAddCallback(dialog, XmNhelpCallback, response, &answer);
  73.         XtManageChild(dialog);
  74.  
  75.         context = XtWidgetToApplicationContext (w);
  76.         while (answer == RET_NONE || XtAppPending(context)) {
  77.                 XtAppProcessEvent (context, XtIMAll);
  78.         }
  79.         XtDestroyWidget(dialog);  /* blow away the dialog box and shell */
  80.         return answer;
  81.   }
  82.  
  83.     The dialog supports three buttons, which are set to call the same 
  84. function when tickled by the user.  The variable answer is set when the user 
  85. finally selects one of those choices:
  86.  
  87.   void response(w, client, call)
  88.         Widget          w;
  89.         XtPointer client;
  90.         XtPointer call;
  91.   {
  92.   int *answer = (int *) client;
  93.   XmAnyCallbackStruct *reason = (XmAnyCallbackStruct *) call;
  94.         switch (reason->reason) {
  95.         case XmCR_OK:
  96.                 *answer = RET_YES;    /* some #define value */
  97.                 break;
  98.         case XmCR_CANCEL:
  99.                 *answer = RET_NO; 
  100.         break;
  101.         case XmCR_HELP:
  102.                 *answer = RET_HELP;
  103.                 break;
  104.         default:
  105.                 return;
  106.         }
  107. }
  108.  
  109. and the code unwraps back to the point at which an answer was needed and
  110. continues from there.
  111.  
  112. [Thanks to Dan Heller (argv@sun.com); note that the code in his book caches
  113. the dialog but neglects to make sure that the callbacks point to the
  114. current automatic "answer".]
  115.  
  116. ----------------------------------------------------------------------
  117. Subject: 135)  How do I determine the name of an existing widget?
  118. I have a widget ID and need to know what the name of that widget is.
  119.  
  120.     Users of R4 and later are best off using the XtName() function, which 
  121. will work on both widgets and non-widget objects.
  122.  
  123.     If you are still using R3, you can use this simple bit of code to do 
  124. what you want. Note that it depends on the widget's internal data structures 
  125. and is not necessarily portable to future versions of Xt, including R4.
  126.  
  127.     #include <X11/CoreP.h>
  128.     #include <X11/Xresource.h>
  129.     String XtName (widget)
  130.     Widget widget;    /* WILL work with non-widget objects */
  131.     {
  132.     return XrmNameToString(widget->core.xrm_name);
  133.     }
  134.  
  135. [7/90; modified with suggestion by Larry Rogers (larry@boris.webo.dg.com) 9/91]
  136.  
  137. ----------------------------------------------------------------------
  138. Subject: 136)  Why do I get a BadDrawable error drawing to XtWindow(widget)?
  139. I'm doing this in order to get a window into which I can do Xlib graphics
  140. within my Xt-based program:
  141.  
  142. > canvas = XtCreateManagedWidget ( ...,widgetClass,...) /* drawing area */
  143. > ...
  144. > window = XtWindow(canvas);    /* get the window associated with the widget */
  145. > ...
  146. > XDrawLine (...,window,...);    /* produces error */
  147.  
  148.     The window associated with the widget is created as a part of the 
  149. realization of the widget.  Using a window id of None ("no window") could 
  150. create the error that you describe.  It is necessary to call XtRealizeWidget() 
  151. before attempting to use the window associated with a widget. 
  152.     Note that the window will be created after the XtRealizeWidget() call, 
  153. but that the server may not have actually mapped it yet, so you should also 
  154. wait for an Expose event on the window before drawing into it.
  155.  
  156. ----------------------------------------------------------------------
  157. Subject: 137)  Where can I get documentation on Xaw, the Athena widget set?
  158.  
  159.     Check ftp.x.org in /pub/R5untarred/mit/hardcopy for the originals
  160. of documentation distributed with X11R5.
  161.  
  162. ----------------------------------------------------------------------
  163. Subject: 138)  What's the difference between actions and callbacks?
  164.  
  165. Actions and callbacks may be closely tied; the user may click a mouse-button
  166. in an object's window, causing an action procedure in that particular object
  167. to be called. As part of its processing of the event, the action procedure
  168. may inform the application via a callback registered on the object. However,
  169. callbacks can be given for any reason, including some that don't arise as a
  170. result of user action; and many actions don't result in any notification to
  171. the application.
  172.  
  173. Callbacks generally are a means of interaction between the user interface
  174. (UI) and some other piece of code interested in the "results"; the interested
  175. party to which the data is communicated is usually the application's back-end
  176. functions but may be another widget in a related part of the UI.  For
  177. example, a text widget invokes a callback to say "the user just entered this
  178. text string; never mind what I had to do to get it or what X events took
  179. place."
  180.  
  181. In object-oriented programming terminology, callback lists are messages
  182. defined by the widget class by which the widget istance notifies another
  183. entity that something significant has happened to the widget.
  184.  
  185. Actions, however, constitute a widget's repertoire of internal i/o
  186. behaviors.  Actions are not about results; actions are about "how", not
  187. "what" gets done. The text widget may define a dozen or two actions which
  188. define how the user can manipulate the text; the procedures for removing a
  189. line of text or switching two words can be associated with particular X event
  190. sequences (and in fact often rely on particular types of events).
  191.  
  192. Actions are (in OOP terminology) methods of the widget class by which the
  193. widget responds to some external stimulus (one or more X events).
  194.  
  195. To avoid confusing yourself on the issue of actions vs. callbacks, try
  196. thinking of actions defined by an application as methods *of the application*
  197. -- applications may define actions, as well -- by which the application
  198. responds to one or more X events (and happens to be handed an object handle
  199. as part of the method argument list). Similarly, callback handlers registered
  200. by an application with a widget can be thought of as methods of the
  201. application which respond to messages from a widget or widgets.
  202.  
  203. [Thanks to Michael Johnson (michael@maine.maine.edu) and to Kerry Kimbrough]
  204.  
  205. ----------------------------------------------------------------------
  206. Subject: 139)  How do I simulate a button press/release event for a widget?
  207.  
  208.     You can do this using XSendEvent(); it's likely that you're not setting
  209. the window field in the event, which Xt needs in order to match to the widget
  210. which should receive the event.
  211.      If you're sending events to your own application, then you can use 
  212. XtDispatchEvent() instead. This is more efficient than XSendEvent() in that you
  213. avoid a round-trip to the server.
  214.     Depending on how well the widget was written, you may be able to call
  215. its action procedures in order to get the effects you want.
  216.  
  217. [courtesy Mark A. Horstman (mh2620@sarek.sbc.com), 11/90]
  218.  
  219. ----------------------------------------------------------------------
  220. Subject: 140)  Can I make Xt or Xlib calls from a signal handler?
  221.  
  222.     No. Xlib and Xt have no mutual exclusion for protecting critical 
  223. sections. If your signal handler makes such a call at the wrong time (which 
  224. might be while the function you are calling is already executing), it can leave
  225. the library in an inconsistent state. Note that the ANSI C standard points
  226. out that behavior of a signal handler is undefined if the signal handler calls
  227. any function other than signal() itself, so this is not a problem specific to
  228. Xlib and Xt; the POSIX specification mentions other functions which may be
  229. called safely but it may not be assumed that these functions are called by 
  230. Xlib or Xt functions.
  231.     You can work around the problem by setting a flag in the interrupt
  232. handler and later checking it with a work procedure or a timer event which
  233. has previously been added.
  234.     R6 Xt will have support for signal handlers; there will be a
  235. mechanism to set a flag in a signal handler and XtAppNextEvent will notice
  236. that the flag has been set, and call the associated callbacks.
  237.  
  238.     Note: the article in The X Journal 1:4 and the example in O'Reilly 
  239. Volume 6 are in error.
  240.  
  241. [Thanks to Pete Ware (ware@cis.ohio-state.edu) and Donna Converse 
  242. (converse@x.org), 5/92]
  243.  
  244. An alternate solution is to create a pipe and add the read side of the pipe
  245. as an input event with XtAppAddInput; then write a byte to the write side of
  246. the pipe with your signal handler (write is re-entrant). The callback for the
  247. read side of the pipe reads the byte and does the actual processing that you
  248. intended. You may want the byte to be the signal number unless your callback
  249. handles only one kind.
  250.  
  251. [Thanks to Steve Kappel (stevek@apertus.com)]
  252.  
  253. ----------------------------------------------------------------------
  254. Subject: 141)  What are these "Xlib sequence lost" errors?
  255.  
  256.     You may see these errors if you issue Xlib requests from an Xlib error 
  257. handler, or, more likely, if you make calls which generate X requests to Xt or 
  258. Xlib from a signal handler, which you shouldn't be doing in any case. 
  259.  
  260. ----------------------------------------------------------------------
  261. Subject: 142)  How can my Xt program handle socket, pipe, or file input?
  262.  
  263.     It's very common to need to write an Xt program that can accept input 
  264. both from a user via the X connection and from some other file descriptor, but 
  265. which operates efficiently and without blocking on either the X connection or 
  266. the other file descriptor.
  267.     A solution is use XtAppAddInput(). After you open your file descriptor,
  268. use XtAppAddInput() to register an input handler. The input handler will be 
  269. called every time there is something on the file descriptor requiring your 
  270. program's attention. Write the input handler like you would any other Xt 
  271. callback, so it does its work quickly and returns.  It is important to use only
  272. non-blocking I/O system calls in your input handlers.
  273.     Most input handlers read the file descriptor, although you can have an 
  274. input handler write or handle exception conditions if you wish.
  275.     Be careful when you register an input handler to read from a disk file.
  276. You will find that the function is called even when there isn't input pending.
  277. XtAppAddInput() is actually working as it is supposed to. The input handler is 
  278. called whenever the file descriptor is READY to be read, not only when there is
  279. new data to be read. A disk file (unlike a pipe or socket) is almost always 
  280. ready to be read, however, if only because you can spin back to the beginning
  281. and read data you've read before.  The result is that your function will almost
  282. always be called every time around XtAppMainLoop(). There is a way to get the 
  283. type of interaction you are expecting; add this line to the beginning of your 
  284. function to test whether there is new data:
  285.          if (ioctl(fd, FIONREAD, &n) == -1 || n == 0) return;
  286. But, because this is called frequently, your application is effectively in a 
  287. busy-wait; you may be better off not using XtAppAddInput() and instead setting 
  288. a timer and in the timer procedure checking the file for input.
  289.  
  290. [courtesy Dan Heller (argv@ora.com), 8/90; mouse@larry.mcrcim.mcgill.edu 5/91;
  291. Ollie Jones (oj@pictel.com) 6/92]
  292.  
  293. There are two alternatives: the simple one is to use XtAppAddTimeout instead
  294. of XtAppAddInput and check for input occasionally; the more complex solution,
  295. and perhaps the better one, is to popen or fork&exec a child which does
  296. blocking reads on the file, relaying what it has read to your application via
  297. a pipe or a socket. XtAppAddInput will work as expected on pipes and
  298. sockets.
  299.  
  300. [Thanks to Kaleb Keithley (kaleb@x.org); 12/93]
  301.  
  302. ----------------------------------------------------------------------
  303. Subject: 143)  Why do I get a BadMatch error when calling XGetImage?
  304.  
  305. The BadMatch error can occur if the specified rectangle goes off the edge of 
  306. the screen. If you don't want to catch the error and deal with it, you can take
  307. the following steps to avoid the error:
  308.  
  309. 1) Make a pixmap the same size as the rectangle you want to capture.
  310. 2) Clear the pixmap to background using XFillRectangle.
  311. 3) Use XCopyArea to copy the window to the pixmap.
  312. 4) If you get a NoExpose event, the copy was clean. Use XGetImage to grab the
  313. image from the pixmap.
  314. 5) If you get one or more GraphicsExpose events, the copy wasn't clean, and 
  315. the x/y/width/height members of the GraphicsExpose event structures tell you 
  316. the parts of the pixmap which aren't good.
  317. 6) Get rid of the pixmap; it probably takes a lot of memory.
  318.  
  319. [10/92; thanks to Oliver Jones (oj@pictel.com)]
  320.  
  321. ----------------------------------------------------------------------
  322. Subject: 144)  How can my application tell if it is being run under X?
  323.  
  324.     A number of programs offer X modes but otherwise run in a straight
  325. character-only mode. The easiest way for an application to determine that it is
  326. running on an X display is to attempt to open a connection to the X server:
  327.     
  328.     display = XOpenDisplay(display_name);
  329.     if (display)
  330.         { do X stuff }
  331.     else
  332.         { do curses or something else }
  333. where display_name is either the string specified on the command-line following
  334. -display, by convention, or otherwise is (char*)NULL [in which case 
  335. XOpenDisplay uses the value of $DISPLAY, if set].
  336.  
  337. This is superior to simply checking for the existence a -display command-line 
  338. argument or checking for $DISPLAY set in the environment, neither of which is 
  339. adequate. [5/91]
  340.  
  341. ----------------------------------------------------------------------
  342. Subject: 145)  How do I make a "busy cursor" while my application is computing?
  343. Is it necessary to call XDefineCursor() for every window in my application?
  344.  
  345.     The easiest thing to do is to create a single InputOnly window that
  346. is as large as the largest possible screen; make it a child of your toplevel
  347. window (which must be realized) and it will be clipped to that window, so it
  348. won't affect any other application. (It needs to be as big as the largest
  349. possible screen in case the user enlarges the window while it is busy or
  350. moves elsewhere within a virtual desktop.) Substitute "toplevel" with your
  351. top-most widget here (similar code should work for Xlib-only applications;
  352. just use your top Window):
  353.  
  354.      unsigned long valuemask;
  355.      XSetWindowAttributes attributes;
  356.  
  357.      /* Ignore device events while the busy cursor is displayed. */
  358.      valuemask = CWDontPropagate | CWCursor;
  359.      attributes.do_not_propagate_mask =  (KeyPressMask | KeyReleaseMask |
  360.          ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
  361.      attributes.cursor = XCreateFontCursor(XtDisplay(toplevel), XC_watch);
  362.  
  363.      /* The window will be as big as the display screen, and clipped by
  364.         its own parent window, so we never have to worry about resizing */
  365.      XCreateWindow(XtDisplay(toplevel), XtWindow(toplevel), 0, 0,
  366.          65535, 65535, (unsigned int) 0, 0, InputOnly,
  367.          CopyFromParent, valuemask, &attributes);
  368.  
  369. where the maximum size above could be replaced by the real size of the screen,
  370. particularly to avoid servers which have problems with windows larger than
  371. 32767.
  372.  
  373. When you want to use this busy cursor, map and raise this window; to go back to
  374. normal, unmap it. This will automatically keep you from getting extra mouse
  375. events; depending on precisely how the window manager works, it may or may not
  376. have a similar effect on keystrokes as well.
  377.  
  378. In addition, note also that most of the Xaw widgets support an XtNcursor 
  379. resource which can be temporarily reset, should you merely wish to change the
  380. cursor without blocking pointer events.
  381.  
  382. [thanks to Andrew Wason (aw@cellar.bae.bellcore.com), Dan Heller 
  383. (argv@sun.com), and mouse@larry.mcrcim.mcgill.edu; 11/90,5/91]
  384.  
  385. ----------------------------------------------------------------------
  386. Subject: 146)  How do I fork without hanging my parent X program?
  387.  
  388.     An X-based application which spawns off other Unix processes which 
  389. continue to run after it is closed typically does not vanish until all of its 
  390. children are terminated; the children inherit from the parent the open X 
  391. connection to the display. 
  392.     What you need to do is fork; then, immediately, in the child process, 
  393.         close (ConnectionNumber(XtDisplay(widget)));
  394. to close the file-descriptor in the display information. After this do your 
  395. exec. You will then be able to exit the parent.
  396.     Alternatively, before exec'ing make this call, which causes the file 
  397. descriptor to be closed on exec.
  398.         (void) fcntl(ConnectionNumber(XDisplay), F_SETFD, 1);
  399.  
  400. [Thanks to Janet Anstett (anstettj@tramp.Colorado.EDU), Gordon Freedman 
  401. (gjf00@duts.ccc.amdahl.com); 2/91. Greg Holmberg (holmberg@frame.com), 3/93.]
  402.  
  403. ----------------------------------------------------------------------
  404. Subject: 147)  Why doesn't anything appear when I run this simple program?
  405.  
  406. > ...
  407. > the_window = XCreateSimpleWindow(the_display,
  408. >      root_window,size_hints.x,size_hints.y,
  409. >      size_hints.width,size_hints.height,BORDER_WIDTH,
  410. >      BlackPixel(the_display,the_screen),
  411. >      WhitePixel(the_display,the_screen));
  412. > ...
  413. > XSelectInput(the_display,the_window,ExposureMask|ButtonPressMask|
  414. >     ButtonReleaseMask);
  415. > XMapWindow(the_display,the_window);
  416. > ...
  417. > XDrawLine(the_display,the_window,the_GC,5,5,100,100);
  418. > ...
  419.  
  420.     You are right to map the window before drawing into it. However, the 
  421. window is not ready to be drawn into until it actually appears on the screen --
  422. until your application receives an Expose event. Drawing done before that will 
  423. generally not appear. You'll see code like this in many programs; this code 
  424. would appear after window was created and mapped:
  425.   while (!done)
  426.     {
  427.       XNextEvent(the_display,&the_event);
  428.       switch (the_event.type) {
  429.     case Expose:     /* On expose events, redraw */
  430.         XDrawLine(the_display,the_window,the_GC,5,5,100,100);
  431.         break;
  432.     ...
  433.     }
  434.     }
  435.  
  436.     Note that there is a second problem: some Xlib implementations don't 
  437. set up the default graphics context to have correct foreground/background 
  438. colors, so this program could previously include this code:
  439.   ...
  440.   the_GC_values.foreground=BlackPixel(the_display,the_screen);    /* e.g. */
  441.   the_GC_values.background=WhitePixel(the_display,the_screen);    /* e.g. */
  442.   the_GC = XCreateGC(the_display,the_window,
  443.                 GCForeground|GCBackground,&the_GC_values);
  444.   ...
  445.  
  446. Note: the code uses BlackPixel and WhitePixel to avoid assuming that 1 is 
  447. black and 0 is white or vice-versa.  The relationship between pixels 0 and 1 
  448. and the colors black and white is implementation-dependent.  They may be 
  449. reversed, or they may not even correspond to black and white at all.
  450.  
  451. Also note that actually using BlackPixel and WhitePixel is usually the wrong 
  452. thing to do in a finished program, as it ignores the user's preference for 
  453. foreground and background.
  454.  
  455. And also note that you can run into the same situation in an Xt-based program
  456. if you draw into the XtWindow(w) right after it has been realized; it may
  457. not yet have appeared.
  458.  
  459. ----------------------------------------------------------------------
  460. Subject: 148)  What is the difference between a Screen and a screen?
  461.  
  462.     The 'Screen' is an Xlib structure which includes the information about
  463. one of the monitors or virtual monitors which a single X display supports. A 
  464. server can support several independent screens. They are numbered unix:0.0,
  465. unix:0.1, unix:0.2, etc; the 'screen' or 'screen_number' is the second digit --
  466. the 0, 1, 2 which can be thought of as an index into the array of available 
  467. Screens on this particular Display connection.
  468.     The macros which you can use to obtain information about the particular
  469. Screen on which your application is running typically have two forms -- one
  470. which takes a Screen and one with takes both the Display and the screen_number.
  471.     In Xt-based programs, you typically use XtScreen(widget) to determine 
  472. the Screen on which your application is running, if it uses a single screen.
  473.     (Part of the confusion may arise from the fact that some of the macros
  474. which return characteristics of the Screen have "Display" in the names -- 
  475. XDisplayWidth, XDisplayHeight, etc.)
  476.     
  477. ----------------------------------------------------------------------
  478. Subject: 149)  Can XGetWindowAttributes get a window's background pixel/pixmap?
  479.  
  480.     No.  Once set, the background pixel or pixmap of a window cannot be 
  481. re-read by clients.  The reason for this is that a client can create a pixmap,
  482. set it to be the background pixmap of a window, and then free the pixmap. The 
  483. window keeps this background, but the pixmap itself is destroyed.  If you're 
  484. sure a window has a background pixel (not a pixmap), you can use XClearArea() 
  485. to clear a region to the background color and then use XGetImage() to read 
  486. back that pixel.  However, this action alters the contents of the window, and 
  487. it suffers from race conditions with exposures. [courtesy Dave Lemke of NCD 
  488. and Stuart Marks of Sun]
  489.  
  490.     Note that the same applies to the border pixel/pixmap. This is a 
  491. (mis)feature of the protocol which allows the server is free to manipulate the
  492. pixel/pixmap however it wants.  By not requiring the server to keep the 
  493. original pixel or pixmap, some (potentially a lot of) space can be saved. 
  494. [courtesy Jim Fulton, then of X Consortium]
  495.  
  496. ----------------------------------------------------------------------
  497. Subject: 150)  How do I create a transparent window?
  498.     
  499.     A completely transparent window is easy to get -- use an InputOnly
  500. window. In order to create a window which is *mostly* transparent, you have
  501. several choices:
  502.     - the SHAPE extension first released with X11R4 offers an easy way to
  503. make non-rectangular windows, so you can set the shape of the window to fit the
  504. areas where the window should be nontransparent; however, not all servers 
  505. support the extension.
  506.     - a machine-specific method of implementing transparent windows for
  507. particular servers is to use an overlay plane supported by the hardware.  Note 
  508. that there is no X notion of a "transparent color index".
  509.     - a generally portable solution is to use a large number of tiny 
  510. windows, but this makes operating on the application as a unit difficult.
  511.     - a final answer is to consider whether you really need a transparent
  512. window or if you would be satisfied with being able to overlay your application
  513. window with information; if so, you can draw into separate bitplanes in colors
  514. that will appear properly.
  515.  
  516. [thanks to der Mouse, mouse@lightning.McRCIM.McGill.EDU, 3/92; see also
  517. The X Journal 1:4 for a more complete answer, including code samples for this
  518. last option]
  519.  
  520. ----------------------------------------------------------------------
  521. Subject: 151)  Why doesn't GXxor produce mathematically-correct color values?
  522.  
  523.     When using GXxor you may expect that drawing with a value of black on a
  524. background of black, for example, should produce white. However, the drawing
  525. operation does not work on RGB values but on colormap indices. The color that
  526. the resulting colormap index actually points to is undefined and visually
  527. random unless you have actually filled it in yourself. [On many X servers Black
  528. and White often 0/1 or 1/0; programs taking advantage of this mathematical
  529. coincidence will break.]
  530.     If you want to be combining colors with GXxor, then you should be 
  531. allocating a number of your own color cells and filling them with your chosen
  532. pre-computed values.
  533.     If you want to use GXxor simply to switch between two colors, then you 
  534. can take the shortcut of setting the background color in the GC (graphics 
  535. context) to 0 and the foreground color to a value such that when it draws over 
  536. red, say, the result is blue, and when it draws over blue the result is red. 
  537. This foreground value is itself the XOR of the colormap indices of red and 
  538. blue.
  539.  
  540. [Thanks to Chris Flatters (cflatter@zia.aoc.nrao.EDU) and Ken Whaley 
  541. (whaley@spectre.pa.dec.com), 2/91]
  542.  
  543. ----------------------------------------------------------------------
  544. Subject: 152)  Why does every color I allocate show up as black?
  545.  
  546.     Make sure you're using 16 bits and not 8.  The red, green, and blue 
  547. fields of an XColor structure are scaled so that 0 is nothing and 65535 is 
  548. full-blast. If you forget to scale (using, for example, 0-255 for each color) 
  549. the XAllocColor function will perform correctly but the resulting color is 
  550. usually black. 
  551.  
  552. [Thanks to Paul Asente, asente@adobe.com, 7/91]
  553.  
  554. ----------------------------------------------------------------------
  555. Subject: 153)  Why do I get a protocol error when creating a cursor (sic)?
  556.  
  557.     You may have had this code working on a monochrome system by
  558. coincidence.  Cursor pixmaps must always have a depth of 1; when you create
  559. the cursor pixmap use the depth of 1 rather than the default depth of the
  560. screen.
  561.  
  562. ----------------------------------------------------------------------
  563. Subject: 154)  Why can't my program get a standard colormap?
  564. I have an image-processing program which uses XGetRGBColormap() to get the 
  565. standard colormap, but it doesn't work. 
  566.  
  567.     XGetRGBColormap() when used with the property XA_RGB_DEFAULT_MAP does 
  568. not create a standard colormap -- it just returns one if one already exists.
  569. Use xstdcmap or do what it does in order to create the standard colormap first.
  570.  
  571. [1/91; from der Mouse (mouse@larry.mcrcim.mcgill.edu)]
  572.  
  573. ----------------------------------------------------------------------
  574. Subject: 155)  Why does the pixmap I copy to the screen show up as garbage? 
  575.  
  576.     The initial contents of pixmaps are undefined.  This means that most
  577. servers will allocate the memory and leave around whatever happens to be there 
  578. -- which is usually garbage.  You probably want to clear the pixmap first using
  579. XFillRectangle() with a function of GXcopy and a foreground pixel of whatever 
  580. color you want as your background (or 0L if you are using the pixmap as a 
  581. mask). [courtesy Dave Lemke of NCD and Stuart Marks of Sun]
  582.  
  583. ----------------------------------------------------------------------
  584. Subject: 156)  How can I most quickly send an image to the X server? 
  585.  
  586.     The fastest mechanism may be to use an XImage and the shared-memory
  587. extension to reduce the transmission time.
  588.     The MIT-SHM code, documentation, and example client programs can be 
  589. found on the X11R5 source tape; many vendors also support the extension.
  590.  
  591. ----------------------------------------------------------------------
  592. Subject: 157)  How do I check whether a window ID is valid?
  593. My program has the ID of a window on a remote display. I want to check whether
  594. the window exists before doing anything with it.
  595.  
  596.     Because X is asynchronous, there isn't a guarantee that the window 
  597. would still exist between the time that you got the ID and the time you sent an
  598. event to the window or otherwise manipulated it. What you should do is send the
  599. event without checking, but install an error handler to catch any BadWindow
  600. errors, which would indicate that the window no longer exists. This scheme
  601. will work except on the [rare] occasion that the original window has been
  602. destroyed and its ID reallocated to another window. 
  603.     You can use this scheme to make a function which checks the validity
  604. of a window; you can make this operation almost synchronous by calling
  605. XSync() after the request, although there is still no guarantee that the
  606. window will exist after the result (unless the sterver is grabbed). On the 
  607. whole, catching the error rather than pre-checking is preferable.
  608.  
  609. [courtesy Ken Lee (klee@synoptics.com), 4/90; 12/93]
  610.  
  611. ----------------------------------------------------------------------
  612. Subject: 158)  Can I have two applications draw to the same window?
  613.  
  614.     Yes. The X server assigns IDs to windows and other resources (actually,
  615. the server assigns some bits, the client others), and any application that 
  616. knows the ID can manipulate the resource [almost any X server resource, except
  617. for GCs and private color cells, can be shared].
  618.     The problem you face is how to disseminate the window ID to multiple 
  619. applications. A simple way to handle this (and which solves the problem of the
  620. applications' running on different machines) is in the first application to 
  621. create a specially-named property on the root-window and put the window ID into
  622. it. The second application then retrieves the property, whose name it also
  623. knows, and then can draw whatever it wants into the window.
  624.     [Note: this scheme works iff there is only one instance of the first
  625. application running, and the scheme is subject to the limitations mentioned
  626. in the Question about using window IDs on remote displays.]
  627.     Note also that you will still need to coordinate any higher-level 
  628. cooperation among your applications. 
  629.     Note also that two processes can share a window but should not try to 
  630. use the same server connection. If one process is a child of the other, it 
  631. should close down the connection to the server and open its own connection.
  632.     Note also that Display IDs and GC values describe addresses local
  633. to an application and cannot be transmitted to another application.
  634.     Note also that several clients may draw to a window but for particular
  635. X events such as button-presses only one client can receive the event.
  636.  
  637. [mostly courtesy Phil Karlton (karlton@wpd.sgi.com) 6/90]
  638.  
  639. ----------------------------------------------------------------------
  640. Subject: 159)  Why can't my program work with tvtwm or swm?
  641.  
  642.     A number of applications, including xwd, xwininfo, and xsetroot, do not
  643. handle the virtual root window which tvtwm and swm use; they typically return 
  644. the wrong child of root. A general solution is to add this code or to use it in
  645. your own application where you would normally use RootWindow(dpy,screen):
  646.  
  647. /* Function Name: GetVRoot
  648.  * Description: Gets the root window, even if it's a virtual root
  649.  * Arguments: the display and the screen
  650.  * Returns: the root window for the client
  651.  */
  652. #include <X11/Xatom.h>
  653. Window GetVRoot(dpy, scr)
  654. Display        *dpy;
  655. int             scr;
  656. {
  657. Window          rootReturn, parentReturn, *children;
  658. unsigned int    numChildren;
  659. Window          root = RootWindow(dpy, scr);
  660. Atom            __SWM_VROOT = None;
  661. int             i;
  662.  
  663.   __SWM_VROOT = XInternAtom(dpy, "__SWM_VROOT", False);
  664.   XQueryTree(dpy, root, &rootReturn, &parentReturn, &children, &numChildren);
  665.   for (i = 0; i < numChildren; i++) {
  666.     Atom            actual_type;
  667.     int             actual_format;
  668.     long            nitems, bytesafter;
  669.     Window         *newRoot = NULL;
  670.  
  671.     if (XGetWindowProperty(dpy, children[i], __SWM_VROOT, 0, 1,
  672.         False, XA_WINDOW, &actual_type, &actual_format, &nitems,
  673.             &bytesafter, (unsigned char **) &newRoot) == Success && newRoot) {
  674.             root = *newRoot;
  675.             break;
  676.         }
  677.     }
  678.  
  679.     return root;
  680. }
  681.  
  682. [courtesy David Elliott (dce@smsc.sony.com). Similar code is in ssetroot, a
  683. version of xsetroot distributed with tvtwm. 2/91]
  684.  
  685. A header file by Andreas Stolcke of ICSI on ftp.x.org:contrib/vroot.h 
  686. functions similarly by providing macros for RootWindow and DefaultRootWindow;
  687. code can include this header file first to run properly in the presence of a
  688. virtual desktop.
  689.  
  690. (Note the possible race condition.)
  691.     
  692. ----------------------------------------------------------------------
  693. Subject: 160)  Can I rely on a server which offers backing store?  
  694.  
  695.     You can assume only that the X server has the capability of doing
  696. backing store and that it might do so and keep your application's visuals
  697. up-to-date without your program's involvement; however, the X server can run
  698. out of resources at any time, so you must be able to handle the exposure
  699. events yourself. You cannot rely on a server which offers backing store to
  700. maintain your windows' contents on your behalf.
  701.  
  702. ----------------------------------------------------------------------
  703. Subject: 161)  How do I catch the "close window" event to avoid "fatal IO error"?
  704.  
  705.     Several windows managers offer a function such as f.kill or f.delete
  706. which sends a message to the application that it should delete its window;
  707. this is usually interpreted as a shutdown message.
  708.     The application needs to catch the WM_DELETE_WINDOW client message.
  709. There is a good example in the xcalc sources in X11R5.
  710.     Motif-based applications should in addition set the resource
  711. XmNdeleteResponse on the top-level shell to XmDO_NOTHING, whether they are
  712. using the Motif window manager or not.
  713.     If the application doesn't handle this message the window manager may
  714. wind up calling XKillClient, which disconnects the client from the display and
  715. typically gives an Xlib error along the lines of "fatal IO error 32 (Broken 
  716. pipe)".
  717.  
  718. [Thanks to Kaleb Keithley, kaleb@x.org; 11/93]
  719.  
  720. ----------------------------------------------------------------------
  721. Subject: 162)  How do I keep a window from being resized by the user?
  722.  
  723.     Resizing the window is done through the window manager; window managers
  724. can pay attention to the size hints your application places on the window, but 
  725. there is no guarantee that the window manager will listen. You can try setting 
  726. the minimum and maximum size hints to your target size and hope for the best. 
  727.     Note that you may wish to reconsider your justification for this
  728. restriction.
  729.  
  730. ----------------------------------------------------------------------
  731. Subject: 163)  How do I keep a window in the foreground at all times?
  732.  
  733.     It's rather antisocial for an application to constantly raise itself
  734. [e.g. by tracking VisibilityNotify events] so that it isn't overlapped -- 
  735. imagine the conflict between two such programs running.  
  736.     The only sure way to have your window appear on the top of the stack
  737. is to make the window override-redirect; this means that you are temporarily
  738. assuming window-management duties while the window is up, so you want to do 
  739. this infrequently and then only for short periods of time (e.g. for popup 
  740. menus or other short parameter-setting windows).
  741.  
  742. [thanks to der Mouse (mouse@larry.mcrcim.mcgill.edu); 7/92]
  743.  
  744. ----------------------------------------------------------------------
  745. Subject: 164)  How do I make text and bitmaps blink in X?
  746.  
  747.     There is no easy way.  Unless you're willing to depend on some sort of
  748. extension (as yet non-existent), you have to arrange for the blinking yourself,
  749. either by redrawing the contents periodically or, if possible, by playing games
  750. with the colormap and changing the color of the contents.
  751.  
  752. [Thanks to mouse@larry.mcrcim.mcgill.edu (der Mouse), 7/91]
  753.  
  754. ----------------------------------------------------------------------
  755. Subject: 165)  How do I get a double-click in Xlib?
  756.  
  757.     Users of Xt have the support of the translation manager to help 
  758. get notification of double-clicking.
  759.     There is no good way to get only a double-click in Xlib, because the 
  760. protocol does not provide enough support to do double-clicks.  You have to do 
  761. client-side timeouts, unless the single-click action is such that you can defer
  762. actually taking it until you next see an event from the server.  Thus, you 
  763. have to do timeouts, which means system-dependent code.  On most UNIXish 
  764. implementations, you can use XConnectionNumber to get the file descriptor of 
  765. the X connection and then use select() or something similar on that.
  766.     Note that many user-interface references suggest that a double-click
  767. be used to extend the action indicated by a single-click; if this is the case
  768. in your interface then you can execute the first action and as a compromise
  769. check the timestamp on the second event to determine whether it, too, should
  770. be the single-click action or the double-click action.
  771.  
  772. [Thanks to mouse@larry.mcrcim.mcgill.edu (der Mouse), 4/93]
  773.  
  774. ----------------------------------------------------------------------
  775. Subject: 166)  How do I render rotated text?
  776.     
  777.     Xlib intentionally does not provide such sophisticated graphics 
  778. capabilities, leaving them up to server-extensions or clients-side graphics
  779. libraries.
  780.     Your only choice, if you want to stay within the core X protocol, is to
  781. render the text into a pixmap, read it back via XGetImage(), rotate it "by 
  782. hand" with whatever matrices you want, and put it back to the server via 
  783. XPutImage(); more specifically:
  784.     1) create a bitmap B and write your text to it.
  785.     2) create an XYBitmap image I from B (via XGetImage).
  786.     3) create an XYBitmap Image I2 big enough to handle the transformation.
  787.     4) for each x,y in I2, I2(x,y) = I(a,b) where 
  788.         a = x * cos(theta) - y * sin(theta)
  789.         b = x * sin(theta) + y * cos(theta)
  790.     5) render I2
  791.     Note that you should be careful how you implement this not to lose
  792. bits; an algorithm based on shear transformations may in fact be better.
  793.     The high-level server-extensions and graphics packages available for X 
  794. also permit rendering of rotated text: Display PostScript, PEX, PHiGS, and GKS,
  795. although most are not capable of arbitrary rotation and probably do not use the
  796. same fonts that would be found on a printer.
  797.     In addition, if you have enough access to the server to install a font
  798. on it, you can create a font which consists of letters rotated at some
  799. predefined angle. Your application can then itself figure out placement of each
  800. glyph.
  801.  
  802. [courtesy der Mouse (mouse@larry.mcrcim.mcgill.edu), Eric Taylor 
  803. (etaylor@wilkins.bmc.tmc.edu), and Ken Lee (klee@synoptics.com), 11/90;
  804. Liam Quin (lee@sq.com), 12/90]
  805.  
  806.     InterViews (C++ UI toolkit, in the X contrib software) has support for
  807. rendering rotated fonts in X.  It could be one source of example code.
  808. [Brian R. Smith (brsmith@cs.umn.edu), 3/91]
  809.     Another possibility is to use the Hershey Fonts; they are 
  810. stroke-rendered and can be used by X by converting them into XDrawLine 
  811. requests. [eric@pencom.com, 10/91]
  812.  
  813.     The xrotfont program by Alan Richardson (mppa3@syma.sussex.ac.uk) 
  814. (posted to comp.sources.x July 14 1992) paints a rotated font by implementing 
  815. the method above and by using an outline (Hershey) font.
  816.     The xvertext package by Alan Richardson (mppa3@syma.sussex.ac.uk) is a 
  817. set of functions to facilitate the writing of text at any angle.  It is on 
  818. ftp.x.org as contrib/xvertext.5.0.shar.Z. 
  819.  
  820.     O'Reilly's X Resource Volume 3 includes information from HP about
  821. modifications to the X fonts server which provide for rotated and scaled text.
  822. The modifications are on ftp.x.org in contrib/hp_xlfd_enhancements.
  823.  
  824. ----------------------------------------------------------------------
  825. Subject: 167)  Why doesn't my multi-threaded X program work (sic) ? 
  826.  
  827. You cannot use non-thread aware, non-reentrant libraries with threads.
  828.  
  829. If you must do this, you have only one choice: call the functions from the
  830. initial thread only.
  831.  
  832. Why opening windows from other threads causes protocol errors can be
  833. explained easily: you are accessing shared resources (the display
  834. structure, the connection to the display, static data in the Xlib) from
  835. a number of threads at the same time, without using any form of
  836. exclusive access control.
  837.  
  838. [Thanks to casper@fwi.uva.nl (Casper H.S. Dik)]
  839.  
  840. Support for multi-threaded X programs (Xlib and Xt) will be in X11R6.
  841.  
  842. ----------------------------------------------------------------------
  843. Subject: 168)  What is the X Registry? (How do I reserve names?)
  844.  
  845.     There are places in the X Toolkit, in applications, and in the X
  846. protocol that define and use string names. The context is such that conflicts
  847. are possible if different components use the same name for different things.
  848.     The X Consortium maintains a registry of names in these domains:
  849. orgainization names, selection names, selection targets, resource types,
  850. application classes, and class extension record types; and several others.
  851.     The list as of 7/91 is in the directory mit/doc/Registry on the R5
  852. tape. The current Registry is also available by sending "send docs registry"
  853. to the xstuff mail server.
  854.     To register names (first come, first served) or to ask questions send 
  855. to xregistry@x.org; be sure to include a postal address for confirmation.
  856.  
  857. [11/90; condensed from Asente/Swick Appendix H; 1/94]
  858. ----------------------------------------------------------------------
  859.  
  860.  
  861. David B. Lewis                     faq%craft@uunet.uu.net
  862.  
  863.         "Just the FAQs, ma'am." -- Joe Friday 
  864. -- 
  865. David B. Lewis        Temporarily at but not speaking for Visual, Inc.
  866. day: dbl@visual.com    evening: david%craft@uunet.uu.net
  867.